/*
 * Copyright (C) 2010 TopCoder Inc., All Rights Reserved.
 */
package com.hercules.xre.protocol.testing.netty.tcp;
import com.hercules.xre.protocol.testing.EventHandler;
import com.hercules.xre.protocol.testing.Helper;
import com.hercules.xre.protocol.testing.netty.BaseProtocolTestingConnectionManager;
import com.hercules.xre.protocol.testing.netty.SSLEngineFactory;

import org.jboss.netty.channel.ChannelPipelineFactory;
/**
 * <p>
 * XRE connection manager that is responsible for handling communication trough TCP.
 * </p>
 * <p>
 * <strong>Thread-safety:</strong> This class isn't thread safe as it's parent isn't thread safe.
 * </p>
 * @author TCSDESIGNER, TCSDEVELOPER
 * @version 1.0
*/
public class TCPProtocolTestingConnectionManager extends BaseProtocolTestingConnectionManager {
    /**
     * <p>
     * Creates new instance.
     * </p>
     * @param sslEngineFactory ssl engine factory, can be null (if null no encryption will be used).
     * @param bindAddress address to which application should be bound,
     *        if null is passed then application will bind on all available interfaces
     * @param port port number on which connection manager should be listening.
     *        A valid port value is between 1 and 65535.
     * @throws IllegalArgumentException when any of passed parameters is invalid
    */
    public TCPProtocolTestingConnectionManager(int port, String bindAddress, SSLEngineFactory sslEngineFactory) {
        super(Helper.checkPortAndLog("TCPProtocolTestingConnectionManager#ctor",
            new String[] {"port", "bindAddress", "sslEngineFactory"},
                new Object[] {port, bindAddress, sslEngineFactory}, LOGGER), bindAddress, sslEngineFactory);
        final String signature = "TCPProtocolTestingConnectionManager#ctor";
        Helper.logExit(signature, null, LOGGER);
    }

    /**
     * <p>
     * Creates new instance of pipeline factory that should be used for handling TCP communication.
     * </p>
     * @param eventHandler event handler that should be used to inform application about communication
     *        events, can't be null
     * @return TCPConnectionChannelPipelineFactory instance.
    */
    protected ChannelPipelineFactory getPipelineFactory(EventHandler eventHandler) {
        final String signature = "TCPProtocolTestingConnectionManager#getPipelineFactory";
        Helper.logEnter(signature, new String[] {"eventHandler"}, new Object[] {eventHandler}, LOGGER);
        ChannelPipelineFactory res = null;
        try {
            Helper.checkNull(eventHandler, "eventHandler", signature, LOGGER);
            res =  new TCPConnectionChannelPipelineFactory(this, eventHandler);
        } finally {
            Helper.logExit(signature, res, LOGGER);
        }
        return res;
    }
}

